import { createRouter, createWebHashHistory } from "vue-router";
import { arrSort } from "@/utils/util";
const modulesFiles = import.meta.glob("./modules/*.js", { eager: true });

export let routers = [];

for (const key in modulesFiles) {
  routers.push(...modulesFiles[key].default);
}

routers = arrSort(routers);

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "根目录",
      meta: {
        title: "根目录",
        requireAuth: false,
        key: "root",
        noTagsView: true,
      },
      redirect: "/loginPage",
    },
    {
      path: "/redirect",
      component: () => import("@/components/LayoutComp/LayoutComp.vue"),
      name: "Redirect",
      meta: {
        title: "redirect",
        requireAuth: false,
        noTagsView: true,
        i18n: "nav.redirect",
        key: "redirect",
      },
      children: [
        {
          path: "/redirect/:path(.*)",
          name: "Redirect",
          component: () => import("@/views/Common/Redirect/Redirect.vue"),
          meta: {},
        },
      ],
    },
    {
      path: "/loginPage",
      name: "登录页",
      meta: {
        title: "登录页",
        requireAuth: false,
        key: "loginPage",
        noTagsView: true,
      },
      component: () => import("@/views/Common/LoginPage/LoginPage.vue"),
    },
    ...routers,
    {
      path: "/Errors",
      component: () => import("@/components/LayoutComp/LayoutComp.vue"),
      meta: {
        title: "错误页面",
        parent: "root",
        key: "errors",
        requireAuth: false,
        noTagsView: true,
      },
      children: [
        {
          path: "/404",
          meta: {
            title: "404页面",
            i18n: "nav.notFound",
            parent: "errors",
            key: "404",
            requireAuth: false,
            showNav: false,
            noTagsView: false,
          },
          component: () => import("@/views/Common/404Page/404Page.vue"),
        },
      ],
    },
    {
      path: "/:pathMatch(.*)",
      name: "mathFalse",
      meta: {
        title: "mathFalse",
        requireAuth: false,
      },
      redirect: "/404",
    },
  ],
});

export default router;
